package com.lagou.service.impl;

import com.lagou.annotation.Autowired;
import com.lagou.annotation.Service;
import com.lagou.annotation.Transactional;
import com.lagou.dao.AccountDao;
import com.lagou.pojo.Account;
import com.lagou.service.TransferService;

import java.sql.SQLException;

/**
 * @author: lcz
 * @date: Created in 18:33 2020/10/4
 * @description:
 */
@Service("transferService")
@Transactional
public class TransferServiceImpl implements TransferService {
    @Autowired
    private AccountDao accountDao;

    /**
     * @param fromCardNo 转出账号
     * @param toCardNo   转入账号
     * @param money      转账金额
     * @author: lcz
     * @date: Created in 18:48 2020/10/4
     * @description: 转账
     */
    @Override
    public void transfer(String fromCardNo, String toCardNo, Integer money) throws SQLException {
        Account fromAccount = accountDao.queryAccountByCardNo(fromCardNo);
        Account toAccount = accountDao.queryAccountByCardNo(toCardNo);

        fromAccount.setMoney(fromAccount.getMoney() - money);
        toAccount.setMoney(toAccount.getMoney() + money);

        accountDao.updateAccountByCardNo(fromAccount);
//        int i = 1/0;
        accountDao.updateAccountByCardNo(toAccount);
    }
}
